package com.example.im.ui.fragment

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.im.R
import com.example.im.callback.OnClickUserListener
import com.example.im.entity.FriendOrRoom
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.UserActivity
import com.example.im.ui.adapter.UserSearchAdapter
import kotlinx.android.synthetic.main.fragment_user_search.*

class UserSearchFragment : AbstractBaseFragment<BasePresenter<*>>(), View.OnClickListener,
    UserActivity.OnToUserSearchFragment,
    UserSearchAdapter.OnClickItemListener {


    var friends = ArrayList<FriendOrRoom>()
    var adapter: UserSearchAdapter? = null

    private var mOnClickUserListener: OnClickUserListener? = null

    companion object {
        fun newInstance(): UserSearchFragment {
            return UserSearchFragment()
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mOnClickUserListener = context as OnClickUserListener
    }

    override val layoutId: Int
        get() = R.layout.fragment_user_search

    override fun initView() {

        adapter = UserSearchAdapter(activity!!, friends)
        adapter!!.setOnItemClickListener(this)
        recycler_view.layoutManager = LinearLayoutManager(activity!!)
        recycler_view.adapter = adapter

        tv_user_search.setOnClickListener(this)
    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {

    }


    override fun onSearchFriendGood(friends: MutableList<FriendOrRoom>) {
        adapter!!.addData(friends)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_user_search ->
                mOnClickUserListener!!.onClickSearch(v, ed_user_id.text.toString())
        }
    }

    override fun invoke(p1: View, p2: Int) {
        mOnClickUserListener!!.onClickFriendItem(p1, friends[p2])
    }

}